package com.theanh.RssActivity.updater;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.util.Log;

import com.theanh.RssActivity.Store;
import com.theanh.RssActivity.db.DbAdapter;
import com.theanh.RssActivity.entity.Item;
import com.theanh.RssActivity.util.*;
public class DbCleaner extends Thread{
	private static final String TAG = "DbCleaner";
	public void run(){
		Log.i(TAG, "deleting item is expired...");
		DbAdapter dbAdapter = null;
		
		try{
			dbAdapter  = DbUtils.createAndOpenDbAdapter();
			Date date = Calendar.getInstance().getTime();
			date.setDate(date.getDate() - Store.setting.getItemExpired());
			List<Item> items = dbAdapter.getItems();
			if (items == null || items.size() == 0)
				return;

			for (Item item : items) {
				if (date.compareTo(item.getPublishDate()) > 0) {
					dbAdapter.deleteItem(item);

					// Delete image's item
					FileUtils.deleteImage(item.getID());
				}
			}
			
//			dbAdapter.deleteItemExpired(Store.setting.getItemExpired());
		}catch(Exception ex){
			Log.e(TAG, ex.toString());
		}finally{
			DbUtils.closeDbAdapter(dbAdapter);
		}
	}
}
